package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

@MultipartConfig
@WebServlet("/upload3")
public class UploadServlet3 extends HttpServlet {

    private final String UPLOAD_PATH = "d:/temp/upload";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 多文件
        Collection<Part> parts = request.getParts();

        for (Part part : parts){
            if (part.getSize() > 0){
                // 获取文件名
                String submittedFileName = part.getSubmittedFileName();
                // 保存
                part.write(UPLOAD_PATH + File.separator + submittedFileName);
            }
        }
    }
}
